# 涉及到__dict__/__setitem__/__getitem__

class Person0:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __setitem__(self, key, value):
        # self.key = value  不能这样写，这样写意味着self增添了属性key
        self.__dict__[key] = value

    def __getitem__(self, item):
        return self.__dict__[item]

p0 = Person0('tom', 20)
print(p0.__dict__)
p0.__dict__['name'] = 'jerry'
print(p0.__dict__)
p0['age'] = 10  # 这样写需要重写__setitem__,相当于对[]输入功能的重载
print(p0.__dict__)
print(p0['age'])  # 这样写需要重写__getitem__,相当于对[]输出功能的重载
